package org.jcnc.views;

import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.jcnc.jnotepad.ui.module.TerminalEmulatorComponent;
import org.jcnc.jnotepad.util.UiUtil;
import org.jcnc.jnotepad.views.root.bottom.function.interfaces.AbstractFunctionChildrenBox;

/**
 * 控制台功能窗口
 *
 * @author gewuyou
 */
public class CmdFunctionBox extends AbstractFunctionChildrenBox {

    private static final CmdFunctionBox INSTANCE = new CmdFunctionBox();

    private CmdFunctionBox() {
        // 切换终端的显示/隐藏状态
        label.setOnMouseClicked(mouseEvent -> toggleTerminal());
    }

    Stage terminalStage = new Stage();
    /**
     * 用于跟踪终端的显示状态
     */
    private boolean terminalVisible = false;


    @Override
    protected String getFunctionName() {
        return "终端";
    }

    public static CmdFunctionBox getInstance() {
        return INSTANCE;
    }

    /**
     * 切换终端的显示/隐藏状态。
     */
    private void toggleTerminal() {
        if (terminalVisible) {
            // 隐藏终端
            terminalVisible = false;
            hideTerminal();
        } else {
            // 显示终端
            terminalVisible = true;
            showTerminal();
        }
    }

    /**
     * 隐藏终端窗口。
     */
    private void hideTerminal() {
        terminalStage.close();
    }

    /**
     * 显示终端窗口。
     */
    private void showTerminal() {
        // 创建一个新的舞台（窗口）
        terminalStage.setTitle("终端");
        terminalStage.getIcons().add(UiUtil.getAppIcon());

        // 创建一个根节点（布局）
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, UiUtil.getAppWindow().getWidth() - 50, UiUtil.getAppWindow().getHeight() / 3);

        // 创建TerminalEmulatorComponent并添加到根节点
        TerminalEmulatorComponent terminal = new TerminalEmulatorComponent();

        root.setCenter(terminal);
        terminalStage.setScene(scene);

        // 显示窗口
        terminalStage.show();
    }
}
